package com.atguigu.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        //主机地址;默认为localhost
        connectionFactory.setHost("localhost");
        //连接端口，默认为5672
        connectionFactory.setPort(5672);
        //虚拟主机名称，默认为/
        connectionFactory.setVirtualHost("/");
        //连接用户，默认为guest
        connectionFactory.setUsername("guest");
        //连接密码。默认为guest
        connectionFactory.setPassword("guest");

        //创建连接
        Connection connection = connectionFactory.newConnection();

        //创建频道
        Channel channel = connection.createChannel();

        //创建队列
        channel.queueDeclare("simple_queue",true,false,false,null);

        String message = "你好，小兔子";

        channel.basicPublish("", "simple_queue", null, message.getBytes());
        System.out.println("已发送消息 = " + message);

        //关闭资源
    }
}
